using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;

namespace UseCaseManager.UI
{
    /// <summary>
    /// A parent-child grid control used for adding and editing Releases and their Drops.
    /// </summary>
    public class ReleasesAndDropsControl : UserControlWin, IFormControl
    {
        private readonly IControlFactory _controlFactory;

        public ReleasesAndDropsControl(IControlFactory controlFactory)
        {
            _controlFactory = controlFactory;

            IReadOnlyGridControl releasesGridControl = SetupParentControl();
            IReadOnlyGridControl dropsGridControl = SetupChildControl();

            // Add the grids to the control. The Releases grid fills the top part of the control.  
            // Change the size here depending on how many items you want to see in this grid.  
            // The Drops grid fills the rest of the control.
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            releasesGridControl.Height = 200;
            layoutManager.AddControl(dropsGridControl, BorderLayoutManager.Position.Centre);
            layoutManager.AddControl(releasesGridControl, BorderLayoutManager.Position.North, true);

            // Connect the Releases and Drops grids so that when an item is selected
            // in the Releases grid, the correct Drops are shown in the child grid.
            releasesGridControl.Grid.BusinessObjectSelected += 
                delegate
                    {
                        Release selectedRelease = releasesGridControl.Grid.SelectedBusinessObject as Release;
                        if (selectedRelease == null) return;
                        dropsGridControl.BusinessObjectCollection=selectedRelease.Drops;
                    };
        }

        private IReadOnlyGridControl SetupParentControl()
        {
            // Create the Releases read-only grid
            IReadOnlyGridControl releasesGridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            releasesGridControl.Initialise(ClassDef.Get<Release>(), "default");

            // Load the collection of Releases.  You can set the criteria and order by values here.
            BusinessObjectCollection<Release> releasesCollection = Broker.GetBusinessObjectCollection<Release>("", "");
            releasesGridControl.BusinessObjectCollection=releasesCollection;

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            releasesGridControl.FilterControl.Visible = false;
            //releasesGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //releasesGridControl.Buttons.ShowDefaultDeleteButton = true;

            return releasesGridControl;
        }

        private IReadOnlyGridControl SetupChildControl()
        {
            // Create the Drops read-only grid to show the child items
            IReadOnlyGridControl dropsGridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            dropsGridControl.Initialise(ClassDef.Get<Drop>(), "default");

            // Creates an empty collection of Drops to put in the child grid
            BusinessObjectCollection<Drop> t2Collection = new BusinessObjectCollection<Drop>();
            dropsGridControl.BusinessObjectCollection = t2Collection;

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            dropsGridControl.FilterControl.Visible = false;
            //dropsGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //dropsGridControl.Buttons.ShowDefaultDeleteButton = true;

            return dropsGridControl;
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form) { }
    }
}
